<template>
    <scrollarea ref="scrollarea" style="width: 604px; height: 580px; padding-left: 6px; padding-top: 6px;">
        <div v-if="#ProjectList == 0" valign="center" align="center" style="width: 332px; height: 50px; background: url(Texture/Aries/Creator/keepwork/ggs/user/tixingzi_332X50_32bits.png#0 0 332 50);"></div>
        <div v-for="project in ProjectList" class="project" v-bind:style="GetProjectStyle(project.id)" v-on:click="ClickProject(project)">
            <div v-bind:style="GetImageStyle(project.extra)">
                <Username style="display: block; height: 30px; width: 264px; position: absolute; top: 104px;"></Username>
            </div>
            <div style="base-font-size: 16px; font-size:16px; color: #522b2b; margin-top: 10px; margin-left:10px; height: 16px; line-height: 16px;">
                <div v-if="GetProjectStatusStyle(project.status)" class="status" style="display:inline-block; width: 16px; height: 16px;" v-bind:style="GetProjectStatusStyle(project.status)"></div>
                <div style="display: inline;">{{GetProjectName(project.name)}}</div>
                <div style="display: inline; margin-left: 10px; color: #999999">{{"#" .. tostring(project.id)}}</div>
            </div>
            <div style="base-font-size: 12px; font-size:12px; color: #999999; margin-top: 10px; margin-left:8px; height: 12px; line-height: 12px;">
                <div style="float: left;">{{GetCreateTime(project.createdAt)}}</div>
                <div style="float: right; margin-right: 16px;">{{GetUpdatedTime(project.updatedAt)}}</div>
            </div>
            <div style="margin-top: 12px; margin-left: 8px; base-font-size: 12px; font-size:12px; color: #999999; ">
                <div style="display:inline; height: 16px;">
                    <div valign="center" style="display:inline; width: 16px; height: 12px; background: url(Texture/Aries/Creator/keepwork/ggs/user/icon_liulan_16X12_32bits.png#0 0 16 12);"></div>
                    <div style="display:inline; line-height: 16px; height: 16px; padding-left: 5px; padding-right: 5px;">{{project.visit or 0}}</div>
                </div>
                <div style="display:inline; height: 16px;">
                    <div style="display:inline; width: 16px; height: 16px; background: url(Texture/Aries/Creator/keepwork/ggs/user/icon_dianzan_16X16_32bits.png#0 0 16 16);"></div>
                    <div style="display:inline; line-height: 16px; height: 16px; padding-left: 5px; padding-right: 5px;">{{project.star or 0}}</div>
                </div>
                <div style="display:inline; height: 16px;">
                    <div style="display:inline; width: 18px; height: 16px; background: url(Texture/Aries/Creator/keepwork/ggs/user/icon_xinxi_18X16_32bits.png#0 0 18 16);"></div>
                    <div style="display:inline; line-height: 16px; height: 16px; padding-left: 5px; padding-right: 5px;">{{project.comment or 0}}</div>
                </div>
            </div>
            <div style="margin-top: 10px; width: 250px;">
                <div style="float: right;">
                    <!-- <div v-if="isAuthUser" v-on:click="ClickOpenWorldDir(project.name)" style="display: inline; margin-left:6px; width: 36px; height: 36px; background: url(Texture/Aries/Creator/keepwork/ggs/user/btn_wenjian_40X40_32bits.png#0 0 40 40);"></div>
                    <div v-if="IsSelected(project.id)" v-on:click="ClickWorldDelete(project.id)" style="display: inline; margin-left:6px; width: 36px; height: 36px; background: url(Texture/Aries/Creator/keepwork/ggs/user/btn_shanchu_40X40_32bits.png#0 0 40 40);"></div> -->
                    <div v-on:click="ClickShareWorld(project.id)" style="display: inline; margin-left:6px; width: 36px; height: 36px; background: url(Texture/Aries/Creator/keepwork/worldshare_32bits.png#109 459 40 40);"></div>
                    <div v-on:click="ClickOpenWorld(project)" style="display: inline; margin-left:6px; width: 36px; height: 36px; background: url(Texture/Aries/Creator/keepwork/ggs/user/btn_jingru_40X40_32bits.png#0 0 40 40);"></div>
                </div>
            </div>
            <Rate v-if="project.rate and project.rate > 0" style="position: absolute; left: 0px;" v-bind:rate="project.rate"></Rate>
            <div v-if="project.visibility == 1" style="position: absolute; top: 5px; right: 5px; width: 28px; height: 31px; background: url(Texture/Aries/Creator/keepwork/ggs/user/suosi_28X31_32bits.png#0 0 28 31);"></div>
        </div>
    </scrollarea>
</template>

<script type="text/lua">
RegisterComponent("Rate", "%ui%/Component/User/Rate.html");
RegisterComponent("Username", "%ui%/Component/User/Username.html");

local WorldList = NPL.load("(gl)Mod/WorldShare/cellar/UserConsole/WorldList.lua");
local KeepworkServiceWorld = NPL.load("(gl)Mod/WorldShare/service/KeepworkService/World.lua");
local UniString = commonlib.gettable("System.Core.UniString");
local selectedProjectId = nil;

function ClickProject(project)
    selectedProjectId = project.id;
    ui:RefreshWindow(0);
end 

function GetProjectStatusStyle(index)
    if(index == 1) then
        return 'background:url(Texture/Aries/Creator/keepwork/worldshare_32bits.png#72 104 16 16);';
    end

    if(index == 2) then
        return 'background:url(Texture/Aries/Creator/keepwork/worldshare_32bits.png#98 104 16 16);';
    end

    if(index == 3) then
        return 'background:url(Texture/Aries/Creator/keepwork/worldshare_32bits.png#20 104 16 16);';
    end

    if(index == 4) then
        return 'background:url(Texture/Aries/Creator/keepwork/worldshare_32bits.png#46 104 16 16);';
    end

    if(index == 5) then
        return 'background:url(Texture/Aries/Creator/keepwork/worldshare_32bits.png#124 104 16 16);';
    end
end

function GetProjectStyle(projectId)
    if (selectedProjectId == projectId) then
        return "background: url(Texture/Aries/Creator/keepwork/ggs/user/zuopkuang_selected_32X32_32bits.png#0 0 32 32:14 14 14 14);";
    else
        return "background: url(Texture/Aries/Creator/keepwork/ggs/user/zuopkuang_32X32_32bits.png#0 0 32 32:14 14 14 14);";
    end
end 

function IsSelected(projectId)
    return isAuthUser and selectedProjectId == projectId;
end 

function GetImageStyle(extra)
    local url = extra and extra.imageUrl or "https://keepwork.com/public/img/project_default_cover_new.af774e7d.png"; 
    return string.format("position: relative; width: 264px; height: 134px; background: url(%s);", url);
end

function GetProjectName(text)
    if type(text) ~= 'string' then
        return ''
    end

    local utf8Text = UniString:new(text)

    if _guihelper.GetTextWidth(text) > 112 then
        return utf8Text:sub(1, 8).text .. '...'
    else
        return text
    end
end

function GetCreateTime(datetime)
    local datestr, timestr = string.match(datetime, "(%d+%D%d+%D%d+)%D*(%d+%D%d+)");
    return datestr .. " " .. timestr;
end

function GetUpdatedTime(datetime)
    local year,month,day,hour,min,sec = string.match(datetime, "(%d+)%D(%d+)%D(%d+)%D+(%d+)%D(%d+)%D(%d+)");
    local dateTime = string.format("%s-%s-%s %s:%s:%s", year,month,day,hour,min,sec);
    local date,time = commonlib.timehelp.GetLocalTime();
    local curDateTime = string.format("%s %s", date, string.gsub(time, "-", ":"));
    local day,hours,minutes,seconds,time_str = commonlib.GetTimeStr_BetweenToDate(curDateTime, dateTime);
    local year = math.floor(day / 365);
    local month = math.floor(day / 30);
    if (year > 0) then return tostring(year) .. " 年前更新" end
    if (month > 0) then return tostring(month) .. " 月前更新" end
    if (day > 0) then return tostring(day) .. " 天前更新" end
    if (hours > 0) then return tostring(hours) .. " 小时前更新" end 
    if (minutes > 0) then return tostring(minutes) .. " 分钟前更新" end 
    if (seconds > 0) then return tostring(seconds) .. " 秒前更新" end 
    return time_str;
end

-- 点击打开世界
function ClickOpenWorld(project)
    ui:CloseWindow();
    GameLogic.RunCommand(string.format("/loadworld %d", project.id));    
    <!-- local worldId, worldName = project.id, project.name;
    local WorldCommon = commonlib.gettable("MyCompany.Aries.Creator.WorldCommon")
    local info = string.format(L"即将离开【%s】进入【%s】", WorldCommon.GetWorldTag("name") or "", worldName);
    _guihelper.MessageBox(info, function(res)
        if(res and res == _guihelper.DialogResult.OK) then
            GameLogic.RunCommand(string.format("/loadworld %d", worldId));    
        end
    end, _guihelper.MessageBoxButtons.OKCancel); -->
end

-- 点击分享世界
function ClickShareWorld(worldId)
    local ShareWorld = NPL.load("(gl)Mod/WorldShare/cellar/ShareWorld/ShareWorld.lua")
    ShareWorld:ShowWorldCode(worldId)
end 

function OnRefresh()
    local scrollarea = GetRef("scrollarea");
    local scrollareaCtrl = scrollarea and scrollarea:GetControl();

    if (scrollareaCtrl) then
        scrollareaCtrl:Connect("onScrollEnd", nil, NextPageProjectList or (function() end), "UniqueConnection");
        local vbar = scrollareaCtrl and scrollareaCtrl.vbar;
        local hbar = scrollareaCtrl and scrollareaCtrl.hbar;
        vbar:SetShowButton(false);
        vbar:SetGrooveBackgroundColor("#ffffff00");
        vbar:SetMinSliderHeight(38);
        vbar:SetSliderBackground("Texture/Aries/Creator/keepwork/ggs/dialog/xiala_12X38_32bits.png#0 0 12 38:2 15 2 15");
    end
end

function ClickWorldDelete(projectId)
    KeepworkServiceWorld:SetWorldInstanceByPid(projectId, function()
        WorldList:DeleteWorld();
    end);
end

function ClickWorldSync(projectId)
    KeepworkServiceWorld:SetWorldInstanceByPid(projectId, function() 
        WorldList:Sync();
    end)
end

-- 打开世界目录
function ClickOpenWorldDir(projectName)
    -- ParaIO.GetCurDirectory(0) .. GameLogic.GetWorld():GetWorldPath();
    local path = ParaIO.GetCurDirectory(0) .. "worlds/DesignHouse/" .. projectName .. "/";
    echo(commonlib.Encoding.Utf8ToDefault(path));
    ParaGlobal.ShellExecute("open", commonlib.Encoding.Utf8ToDefault(path), "", "", 1);
end

</script>

<style type="text/css">  
.project {
    position: relative;
    display: inline-block; 
    margin:3px; 
    padding: 10px; 
    padding-top: 8px; 
    padding-bottom: 9px; 
    color:white;
    width: 284px;
    height: 274px;
    background: url(Texture/Aries/Creator/keepwork/ggs/user/zuopkuang_32X32_32bits.png#0 0 32 32:14 14 14 14);
}

.status {
    width: 16px;
    height: 16px;
    margin-left: -2px;
    margin-top: 2px;
    margin-right: 4px;
}
</style>

<!-- background: url(Texture/Aries/Creator/keepwork/ggs/user/zuopkuang_32X32_32bits.png#0 0 32 32:14 14 14 14); -->